package com.hdoit.ares.logical.ser.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.logical.superbase.model.RoleEmployee;
import com.tennetcn.common.logical.superbase.service.IEmployeeService;
import com.tennetcn.common.logical.superbase.service.IRoleEmployeeService;
import com.tennetcn.common.logical.superbase.service.IRoleService;
import com.tennetcn.common.message.DaoBaseRuntimeException;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.alibaba.druid.util.StringUtils;
import com.hdoit.ares.logical.ser.dao.IStaffDao;
import com.hdoit.ares.logical.ser.model.Staff;
import com.hdoit.ares.logical.ser.service.IStaffService;
import com.hdoit.ares.logical.viewmodel.ser.StaffSearch;
/**
 * @author      lvsi
 * @email       
 * @createtime  2017年11月29日 09:12:44
 * @comment
 */
@Component
public class StaffServiceImpl extends SuperService<Staff> implements IStaffService{

	private IStaffDao staffDao;

	public IStaffDao getStaffDao() {
		return staffDao;
	}

	@Resource
	public void setStaffDao(IStaffDao staffDao) {
		this.staffDao = staffDao;
	}

	private IEmployeeService employeeService;

	public IEmployeeService getEmployeeService() {
		return employeeService;
	}

	@Resource
	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	private IRoleService roleService;

	public IRoleService getRoleService() {
		return roleService;
	}

	@Resource
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}

	private IRoleEmployeeService roleEmployeeService;

	public IRoleEmployeeService getRoleEmployeeService() {
		return roleEmployeeService;
	}

	@Resource
	public void setRoleEmployeeService(IRoleEmployeeService roleEmployeeService) {
		this.roleEmployeeService = roleEmployeeService;
	}


	@Override
	public boolean applyChange(Staff staff) {
		if(employeeService.applyEmployeeLogin(staff)){
			if(!super.applyChange(staff)){
				throw new DaoBaseRuntimeException("employeeUser applyChange roll back");
			}
			return saveRoleStaff(staff);
		}
		return false;
	}

	private boolean saveRoleStaff(Staff staff) {
		if (staff.getRoleIds() == null) {
			return true;
		}
		roleEmployeeService.deleteByEmployeeId(staff.getEmployeeId());
		return saveRoleStaff(staff.getRoleIds(), staff);
	}

	private boolean saveRoleStaff(String roleIds, Staff staff) {
		if (StringUtils.isEmpty(roleIds)) {
			return true;
		}
		String[] roleIdStrs = roleIds.split(",");
		List<RoleEmployee> roleEmployees = new ArrayList<RoleEmployee>();
		RoleEmployee roleEmployee = null;
		for (String roleId : roleIdStrs) {
			roleEmployee = new RoleEmployee();
			roleEmployee.setId(CommonUtils.getKeyId());
			roleEmployee.setRoleId(roleId);
			roleEmployee.setEmployeeId(staff.getEmployeeId());
			roleEmployee.setModelStatus(ModelStatus.add);
			roleEmployee.setDeleteMark(YesOrNoInteger.no);
			if (ModelStatus.add.equals(staff.getModelStatus())) {
				roleEmployee.setCreateDate(staff.getCreateDate());
				roleEmployee.setCreateUserId(staff.getCreateUserId());
				roleEmployee.setCreateUserName(staff.getCreateUserName());
			} else if (ModelStatus.update.equals(staff.getModelStatus())) {
				roleEmployee.setCreateDate(staff.getModifyDate());
				roleEmployee.setCreateUserId(staff.getModifyUserId());
				roleEmployee.setCreateUserName(staff.getModifyUserName());
			}
			roleEmployees.add(roleEmployee);
		}
		return roleEmployeeService.insertListEx(roleEmployees) == roleEmployees.size();
	}

	@Override
	public int queryStaffCountById(StaffSearch staffSearch, String loginId) {
		
		return staffDao.queryStaffCountById(staffSearch, loginId);
	}

	@Override
	public List<Staff> queryStaffListById(StaffSearch staffSearch,PagerModel pagerModel, String loginId) {
		
		return staffDao.queryStaffListById(staffSearch, pagerModel, loginId);
	}

	@Override
	public List<Staff> queryStaffById(String loginId) {
		
		return staffDao.queryStaffById(loginId);
	}

	@Override
	public List<Staff> queryAllStaff(StaffSearch staffSearch,PagerModel pagerModel) {
		
		return staffDao.queryAllStaff(staffSearch, pagerModel);
	}

	@Override
	public int queryAllStaffCount(StaffSearch staffSearch,PagerModel pagerModel) {

		return staffDao.queryAllStaffCount(staffSearch, pagerModel);
	}
}

